// pages/login/login.js
import { $wuxForm } from '../../dist/index'
import { $wuxToast } from '../../dist/index'
const network = require('../../utils/network.js')
var app = getApp();     // 取得全局App
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onSubmit_Login: function (e) {
    var that = this;
    const { getFieldsValue, getFieldValue, setFieldsValue } = $wuxForm();
    const value = getFieldsValue()
    if (value.username === "" || value.username===null) {
      // wx.showToast({
      //   title: '用户名不能为空！',
      // })
      $wuxToast().show({
        type: 'text',
        duration: 1500,
        color: '#fff',
        text: '用户名不能为空！',
        success: () => console.log('用户名不能为空！'),
      })
      return;
    } 
    if (value.password === "" || value.password===null) {
      $wuxToast().show({
        type: 'text',
        duration: 1500,
        color: '#fff',
        text: '密码不能为空！',
        success: () => console.log('密码不能为空！'),
      })
      return;
    }
    //加载提示框
    wx.showLoading("登录中...");

    that.myRequest(value)
  


  },

  myRequest: function (value){
    var that = this;
    //console.log('Wux Form Submit \n', value)
    network.post('/user/login?usercode=' + value.username + '&password=' + value.password, {}).then(function (res) {
      console.log('res:', res)
      wx.hideLoading()
      if (res.data.code == "200") {
        var token = res.data.token;
        var components = res.data.components
        app.globalData.user = value
        app.globalData.token = res.data.token
        app.globalData.components = components
        wx.setStorageSync('token', token);
        wx.setStorageSync('user', value);
       // wx.setStorageSync('components', components);
        // console.log('app.globalData.components:', app.globalData.components)
        wx.switchTab({
          url: '../index/index',
        })
      } else {
        console.log('失败！')
        wx.showModal({

          title: '提示',

          content: '账号或密码错误请重新输入！',

        })
      }

    }).then(function (res) {



    }).catch(function (res) {
      console.log(res)
    })

  },
  onChange(e) {
    const { form, changedValues, allValues } = e.detail

    console.log('onChange \n', changedValues, allValues)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})